package com.gp.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gp.dao.AdvertiseDao;
import com.gp.entity.Advertise;
import com.gp.util.CommonResult;

import com.gp.service.AdvertiseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class AdvertiseServiceImpl implements AdvertiseService {
    @Resource
    private AdvertiseDao advertiseDao;

    @Override
    public CommonResult findAll(Integer page, Integer limit, Advertise advertise) {
        Page<Advertise> advertisePage = new Page<>(page, limit);
        QueryWrapper<Advertise> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(advertise.getName())){
            wrapper.like("name",advertise.getName());
        }
        Page<Advertise> selectPage = advertiseDao.selectPage(advertisePage, wrapper);

        return new CommonResult(200, "操作成功", selectPage);
    }
    //删除
    @Override
    public CommonResult deleteById(Integer id) {
        int i = advertiseDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "操作成功", null);
        } else {
            return new CommonResult(500, "操作失败", null);
        }
    }

    //根据id修改信息
    @Override
    public CommonResult updateById(Advertise advertise) {
        int i = advertiseDao.updateById(advertise);
        if (i > 0) {
            return new CommonResult(200, "操作成功", null);
        } else {
            return new CommonResult(500, "操作失败", null);
        }
    }

    @Override
    public CommonResult insert(Advertise advertise) {
        int insert = advertiseDao.insert(advertise);

        if (insert > 0) {
            return new CommonResult(200, "操作成功", null);
        } else {
            return new CommonResult(500, "操作失败", null);
        }
    }


}
